//
//  MM_LoginCell.m
//  MallManage_IOS
//
//  Created by 张浩 on 2023/9/12.
//

#import "MM_LoginCell.h"

@interface MM_LoginCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UIView *baseView;
@property (nonatomic, strong) UIImageView *leftImageView;
@property (nonatomic, strong) UITextField *textField;

@end

@implementation MM_LoginCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
                
        _baseView = [[UIView alloc] init];
        _baseView.backgroundColor = [ColorUtil colorWithHexadecimal:@"#ffffff"];
        _baseView.layer.cornerRadius = 5;
        _baseView.layer.masksToBounds = YES;
        _baseView.layer.borderWidth = 0.5;
        _baseView.layer.borderColor = MM_ColorUtil(@"#1F71C4").CGColor;
        [self.contentView addSubview:_baseView];
        
        _leftImageView = [[UIImageView alloc] init];
        [self.baseView addSubview:_leftImageView];
        
        _textField = [[UITextField alloc] init];
        _textField.delegate = self;
        _textField.textAlignment = NSTextAlignmentLeft;
        _textField.keyboardType = UIKeyboardTypeDefault;
        _textField.font = FONT(14);
        _textField.textColor = [ColorUtil colorWithHexadecimal:@"#333333"];
        [_textField addTarget:self action:@selector(nameTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [self.baseView addSubview:_textField];
        
        [self setUpViews];
        
    }
    return self;
}

- (void)setUpViews {
    
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(38);
        make.right.equalTo(-38);
        make.top.equalTo(self.contentView);
        make.height.equalTo(50);
        make.bottom.equalTo(self.contentView).offset(-22);
    }];
    
    [self.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(5);
        make.centerY.equalTo(self.baseView);
        make.width.height.equalTo(22);
    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.leftImageView.mas_right).offset(5);
        make.centerY.height.equalTo(self.baseView);
        make.right.equalTo(-20);
    }];
}

- (void)setModel:(MM_LoginModel *)model {
    _model = model;
    self.textField.secureTextEntry = NO;

    self.leftImageView.image = MM_Image(model.imageStr);
    self.textField.placeholder = model.placeholderText;
    
    if (!stringIsEmpty(model.inputText)) {
        self.textField.text = model.inputText;
    }
    
    if (model.loginType == MM_LoginTypePassword) {
        self.textField.secureTextEntry = YES;
    }
}

- (void)nameTextFieldDidChange:(UITextField *)textField {
    
    self.model.inputText = textField.text;
}

@end
